define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'projectexpert/index' + location.search,
                    // add_url: 'projectexpert/add',
                    // edit_url: 'projectexpert/edit',
                    // del_url: 'projectexpert/del',
                    multi_url: 'projectexpert/multi',
                    import_url: 'projectexpert/import',
                    table: 'projectexpert',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'e.name', title: "专家名称",operate: 'LIKE',formatter:function(e,row){
                            return row.name;
                        }},
                        {field: 'e.contact_tel', title: "联系方式",operate: 'LIKE',formatter:function(e,row){
                            return row.contact_tel;
                        }},
                        {field: 'e.professional', title: "专家行业",operate: 'LIKE',formatter:function(e,row){
                            return row.professional;
                        }},
                        {field: 'status', title:"状态" ,searchList: {"1":"待回复", "2":"同意","3":"拒绝"}, formatter: Table.api.formatter.label},
                        {field: 'sms_status', title:"短信发送状态",searchList: {"1":"待发送", "2":"发送成功","3":"发送失败", "4":"已收到回执","5":"未收到回执"},formatter: Table.api.formatter.label},
                        {field: 'send_time', title:"短信发送时间",addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}, 
                        // {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [ 
                                {
                                    name: 'agree',
                                    title: "同意",
                                    text:"同意",
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    // icon: 'fa fa-magic',
                                    confirm: '确认同意吗？',
                                    url: 'projectexpert/agree?id={row.id}',
                                    success: function (data, ret) {
                                        window.location.href = data;
                                         
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        // Toastr.success('操作成功');
                                    },
                                    error: function (data, ret) { 
                                        // console.log(data, ret);
                                        // Layer.alert('打印失败');
                                        Toastr.error(ret.msg);
                                        // Toastr.error('操作失败');
                                        return false;
                                    },
                                    // visible:function(row){ 
                                    //     if(row.status != 1){
                                    //         return true
                                    //     }else{
                                    //         return false
                                    //     }
                                    // },
                                }, 
                                {
                                    name: 'refuse',
                                    title: "拒绝",
                                    text:"拒绝",
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    // icon: 'fa fa-magic',
                                    confirm: '确认拒绝吗？',
                                    url: 'projectexpert/refuse?id={row.id}',
                                    success: function (data, ret) { 
                                        window.location.href = data; 
                                    },
                                    error: function (data, ret) { 
                                        Toastr.error(ret.msg);
                                        return false;
                                    }
                                }, 
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'projectexpert/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'projectexpert/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'projectexpert/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
